package com.hrt.datasender.datadao.model;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.btcode.exception.BusinessException;
import com.btcode.orm.model.IDomainField;
import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModelAddition;
import com.btcode.orm.relation.ex.IRelation;
import com.hrt.datasender.datadao.service.BcSerialService;

public class BcSerialModelAddition implements IModelAddition<BcSerialModel> {

    @Override
    public void generateDefaultValue(BcSerialModel model) {
        BcSerialModel value = (BcSerialModel) model;
        value.setId(UUID.randomUUID().toString());
        value.setCreatetime(new Date());
    }

    @Override
    public void initField(List<IField> fields) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initRelations(List<IRelation> relations) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initDomainField(List<IDomainField> domainFields) {
        // TODO Auto-generated method stub

    }

    /**
     * <pre>
     * 注释
     * </pre>
     * 
     * @author FAN
     * @date 2018年9月12日下午5:23:54
     * @param model
     */
    @Override
    public void beforeCreate(BcSerialModel model) {

        List<BcSerialModel> list = BcSerialService.getInstance().getSerials();

        if (model.getComNum() == null || model.getComNum().equals("")) {
            throw new BusinessException("串口号不能为空");
        }
        else if (model.getParity() == null || model.getParity().equals("")) {
            throw new BusinessException("奇偶性校验不能为空");
        }
        else if (model.getDataBit() == null || model.getDataBit().equals("")) {
            throw new BusinessException("数据位不能为空");
        }
        else if (model.getBaudRate() == null || model.getBaudRate().equals("")) {
            throw new BusinessException("波特率不能为空");
        }
        else if (model.getDataProtocol() == null || model.getDataProtocol().equals("")) {
            throw new BusinessException("数据协议不能为空");
        }
        else if (model.getStopBit() == null || model.getStopBit().equals("")) {
            throw new BusinessException("停止位不能为空");
        }

        for (int i = 0; i < list.size(); i++) {
            if (model.getComNum().equals(list.get(i).getComNum())) {
                throw new BusinessException("串口号不能重复");
            }
        }

    }

    @Override
    public void afterCreate(BcSerialModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeUpdate(BcSerialModel model) {

        List<BcSerialModel> list = BcSerialService.getInstance().getSerials();

        for (int i = 0; i < list.size(); i++) {

            String modelId = model.getId();
            String listId = list.get(i).getId();

            if (!modelId.equals(listId)) {
                if (model.getComNum().equals(list.get(i).getComNum())) {
                    throw new BusinessException("串口号不能重复");
                }
            }
        }

    }

    @Override
    public void afterUpdate(BcSerialModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeDelete(BcSerialModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterDelete(BcSerialModel model) {
        // TODO Auto-generated method stub

    }

}
